<?php
namespace frontend\controllers;

use Yii;
use common\models\Needs;
use common\models\search\NeddsSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * NeedsController implements the CRUD actions for Needs model.
 */
class NeedsController extends Controller
{

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => [
                        'POST'
                    ]
                ]
            ]
        ];
    }

    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction'
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'maxLength' => 4,
                'minLength' => 4
            ]
        ];
    }

    /**
     * Lists all Needs models.
     * 
     * @return mixed
     */
    public function actionIndex()
    {
        return $this->render('index', [
            'model' => Needs::find()->asArray()
                ->all()
        ]);
    }

    /**
     * Displays a single Needs model.
     * 
     * @param integer $id            
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id)
        ]);
    }

    /**
     * Creates a new Needs model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * 
     * @return mixed
     */
    public function actionCreate()
    {
        if (! Yii::$app->user->id) {
            return $this->redirect([
                'index',
                'login' => 1
            ]);
        }
        $model = new Needs();
        
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect([
                'index'
            ]);
        } else {
            return $this->render('create', [
                'model' => $model
            ]);
        }
    }

    /**
     * Updates an existing Needs model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * 
     * @param integer $id            
     * @return mixed
     */
    // public function actionUpdate($id) {
    // $model = $this->findModel($id);
    //
    // if ($model->load(Yii::$app->request->post()) && $model->save()) {
    // return $this->redirect(['view', 'id' => $model->id]);
    // } else {
    // return $this->render('update', [
    // 'model' => $model,
    // ]);
    // }
    // }
    //
    // /**
    // * Deletes an existing Needs model.
    // * If deletion is successful, the browser will be redirected to the 'index' page.
    // * @param integer $id
    // * @return mixed
    // */
    // public function actionDelete($id) {
    // $this->findModel($id)->delete();
    //
    // return $this->redirect(['index']);
    // }
    
    /**
     * Finds the Needs model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * 
     * @param integer $id            
     * @return Needs the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Needs::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
